package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jstyle.jclifexd.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/5/15.
 */

public class YogaAdapter extends RecyclerViewBaseAdapter {
    private static final String TAG = "YogaAdapter";
    int mRecyclerViewWidth;
    public YogaAdapter(int mRecyclerViewWidth) {
        super(mRecyclerViewWidth);
       this. mRecyclerViewWidth =mRecyclerViewWidth;
    }

    @Override
    protected void bindData(BaseViewHolder holder, final int position) {
        Log.i(TAG, "bindData: "+position);
        final YogaViewHolder yogaViewHolder=(YogaViewHolder)holder;
       final Context context=holder.itemView.getContext();
        String name=position==selectedPosition?"yoga_"+position+"_selected":"yoga_"+position;
        int id=context.getResources().getIdentifier(name, "drawable",context.getPackageName());
        yogaViewHolder.ivYoga.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemClickListener!=null&&selectedPosition!=position){
                    //selectedPosition=position;
                    itemClickListener.onItemClick(position);
                    //yogaViewHolder.ivYoga.setImageResource(context.getResources().getIdentifier("yoga_"+position+"_selected", "drawable",context.getPackageName()));
                   // notifyDataSetChanged();
                }
            }
        });

        yogaViewHolder.ivYoga.setImageResource(id);

    }
    private int selectedPosition=-1;
    public void setSelected(int position){
        if(selectedPosition==position)return;
        selectedPosition=position;
        notifyDataSetChanged();
    }
    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_yoga;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new YogaViewHolder(getInflaterView(parent, viewType));
    }


    class YogaViewHolder extends BaseViewHolder {
        @BindView(R.id.iv_yoga)
        ImageView ivYoga;


        public YogaViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        return 16;
    }
    ItemClickListener itemClickListener;
    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener=itemClickListener;
    }
    public interface ItemClickListener{
        public void onItemClick(int position);
    }
}
